<?php echo $this->render('/public/header'); ?>
<script src="https://cdn.bootcss.com/echarts/3.8.5/echarts.min.js"></script>
<script src="https://cdn.bootcss.com/element-ui/1.4.2/index.js"></script>
<link href="https://cdn.bootcss.com/element-ui/1.4.2/theme-default/index.css" rel="stylesheet">
<div id="main" class="col-lg-12">
    <div class="col-lg-3 col-md-3 col-xs-3 col-sm-3" id="date">
        <el-date-picker
                v-model="date"
                type="month"
                placeholder="选择月"
                :onClick="this.handlechange()">
        </el-date-picker>
    </div>
    <div role="group" aria-label="..." class="btn-group">
        <button type="button" onclick="show(1)" class="btn btn-default">显示表格</button>
        <button type="button" onclick="show(2)" class="btn btn-default">显示折线图</button>
    </div>
    <div class="col-lg-10 col-lg-offset-2 col-md-10 col-md-offset-2 col-xs-10 col-xs-offset-2">
        <?php if (isset($this->params['list'])) {
            foreach ($this->params['list'] as $k => $v) { ?>
                <div style="float:left;width:140px;height:170px;margin:5px;border:2px solid #fcfcfc">
                    <div>
                        <p><?php echo $k; ?></p>
                        <p>新询盘<?php echo $v['num']; ?></p>
                        <p>分配<?php echo $v['status1']; ?></p>
                        <p>拒绝<?php echo $v['status2']; ?></p>
                        <?php foreach ($v['saleman'] as $k2 => $v2) { ?>
                            <p><?php echo $k2 . '--' . $v2['num'] . ''; ?></p>
                        <?php } ?>
                    </div>
                </div>
            <?php }
        } ?>
    </div>
    <div class="col-lg-12" id="table">
        数据解释//询盘数量-未下单-样品单-已下单
        <br/>
        <br/>
        <table class="table table-borderd">
            <thead>
            <tr>
                <th>业务员</th>
                <?php $month_sum = [];
                $month_list = [];
                $i = $this->params['year'] >= date('Y') ? date('m') : 12;
                for (; $i > 0; $i--) { ?>
                    <th><?php
                        $month = $this->params['year'] . '-' . str_pad($i, 2, 0, STR_PAD_LEFT);
                        $month_list[] = $month;
                        $month_sum[$month] = [1 => 0, 2 => 0, 3 => 0, 'sum' => 0];
                        echo $month; ?></th>
                <?php } ?>
                <th>统计</th>
            </tr>
            </thead>
            <?php foreach ($this->params['month_list'] as $k => $v) { ?>
            <tr>
                <td><?php echo $k; ?></td>
                <?php $saleman = [1 => 0, 2 => 0, 3 => 0]; ?>
                <?php foreach ($month_list

                as $v2): ?>

                <td style="text-align: right">
                    <?php
                    $month = $v2;
                    echo isset($v[$month]) ? "<span class='badge'>{$v[$month]['num']}</span>" : "";

                    if (isset($v[$month]))
                    {
                    $arr = explode(',', $v[$month]['order_status']);
                    $count_arr = array_count_values($arr);
                    $num_arr = [];

                    $not_order = isset($count_arr[1]) ? $count_arr[1] : 0;
                    $yp = isset($count_arr[2]) ? $count_arr[2] : 0;
                    $order = isset($count_arr[3]) ? $count_arr[3] : 0;

                    $num_arr[] = $not_order;
                    $num_arr[] = $yp;
                    $num_arr[] = $order;
                    $saleman[1] += $not_order;
                    $saleman[2] += $yp;
                    $saleman[3] += $order;
                    $month_sum[$month][1] += $not_order;
                    $month_sum[$month][2] += $yp;
                    $month_sum[$month][3] += $order;
                    $month_sum[$month]['sum'] += $v[$month]['num'];
                    ?>
                    <div>
                        <?php foreach ($num_arr as $k2 => $v2) { ?>
                            <?php foreach ($v as $vv) {
                                $service_name = $vv['service_name'] ?? '';
                            }; ?>
                            <?php echo "<a target='__blank' href='/setting/inquiry_list?end_time=" . $month . "-31&begin_time=" . $month . "-01&service_name=" . $service_name . "&property=" . ($k2 + 1) . "' style='display:inline-block;width:30px'>" . $v2 . "</a>"; ?>
                        <?php } ?>
                    </div>
                    <?php foreach ($num_arr as $k2 => $v2) { ?>
                        <?php echo "<p style='display:inline-block;width:30px'>" . round(($v2 / $v[$month]['num'] * 100), 0) . "%" . "</p>"; ?>
                    <?php } ?>
    </div>
    <?php } ?>
    </td>
    <?php endforeach; ?>
    <td style="text-align: right">
        <span class='badge'><?php echo($saleman[1] + $saleman[2] + $saleman[3]); ?></span>
        <div>
            <div>
                <?php foreach ($saleman as $k2 => $v2) { ?>
                    <?php echo "<a target='__blabk' href='/setting/inquiry_list?end_time={$this->params['year']}-12-31&begin_time={$this->params['year']}-01-01&service_name=" . $service_name . "&property=" . ($k2) . "' style='display:inline-block;width:30px'>" . $v2 . "</a>"; ?>
                <?php } ?>
            </div>
        </div>
        <div>
            <?php foreach ($saleman as $k2 => $v2) { ?>
                <?php echo "<p style='display:inline-block;width:30px'>" . round(($v2 / ($saleman[1] + $saleman[2] + $saleman[3]) * 100), 0) . "%" . "</p>"; ?>
            <?php } ?>
        </div>
    </td>
    </tr>
    <?php } ?>
    <tr>
        <td>统计</td>
        <?php $all = 0;
        $all_arr = [1 => 0, 2 => 0, 3 => 0];
        foreach ($month_sum as $k => $v) { ?>
            <td style="text-align: right">
                <span class='badge'><?php echo $v['sum']; ?></span>
                <div>
                    <?php
                    $all += $v['sum'];
                    $all_arr[1] += $v[1];
                    $all_arr[2] += $v[2];
                    $all_arr[3] += $v[3];
                    ?>
                    <?php foreach ($v as $k2 => $v2) { ?>
                        <?php if (!is_numeric($k2)) {
                            continue;
                        }
                        if ($v['sum'] == 0) {
                            continue;
                        } ?>
                        <?php echo "<p style='display:inline-block;width:30px'>" . ($v['sum'] ? round(($v2 / ($v['sum']) * 100), 0) : 0) . "%" . "</p>"; ?>
                    <?php } ?>
                </div>
            </td>
        <?php } ?>
        <td style="text-align: right">
            <span class='badge'><?php echo $all; ?></span>
            <div>
                <?php foreach ($all_arr as $k2 => $v2) { ?>
                    <?php echo "<p style='display:inline-block;width:30px'>" . ($all ? round(($v2 / ($all) * 100), 0) : 0) . "%" . "</p>"; ?>
                <?php } ?>
            </div>
        </td>
    </tr>
    </table>
</div>
<div id="chartConatiner" style="height:600px;" class="col-lg-12 col-xs-12 col-sm-12 col-md-12">
    <div id="chart" style="height:600px" class="col-lg-6 col-xs-6 col-sm-6 col-md-6"></div>
    <div id="chart2" style="height:600px" class="col-lg-6 col-xs-6 col-sm-6 col-md-6"></div>
</div>
</div>
<script>
    var vm = new Vue({
        el: "#date",
        data: {
            date: ""
        },
        methods: {
            handlechange: function () {
                if (this.date) {
                    var now = new Date(this.date);
                    var str = now.getFullYear() + '-' + (now.getMonth() + 1);
                    window.location.href = "/table/inquiry?date=" + str;
                }
            }
        }
    })

    var myChart = echarts.init(document.getElementById('chart'));

    // 指定图表的配置项和数据
    var option = {
        title: {
            text: '询盘月度有效数量',
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            x: 'left',
            data:<?php echo json_encode($this->params['title']);?>
        },
        series: [
            {
                name: '所有询盘',
                type: 'pie',
                radius: ['50%', '70%'],
                avoidLabelOverlap: false,
                label: {
                    normal: {
                        show: false,
                        position: 'center'
                    },
                    emphasis: {
                        show: true,
                        textStyle: {
                            fontSize: '30',
                            fontWeight: 'bold'
                        }
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                data:<?php echo json_encode(($this->params['chartData']['list'] ?? ""));?>
            }
        ]
    };


    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);

    var myChart2 = echarts.init(document.getElementById('chart2'));

    // 指定图表的配置项和数据
    var option2 = {
        title: {
            text: '询盘月度数量',
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            x: 'left',
            data:<?php echo json_encode($this->params['title']);?>
        },
        series: [
            {
                name: '所有询盘',
                type: 'pie',
                radius: ['50%', '70%'],
                avoidLabelOverlap: false,
                label: {
                    normal: {
                        show: false,
                        position: 'center'
                    },
                    emphasis: {
                        show: true,
                        textStyle: {
                            fontSize: '30',
                            fontWeight: 'bold'
                        }
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                data:<?php echo json_encode($this->params['chartData']['list2'] ?? "");?>
            }
        ]
    };
    myChart2.setOption(option2);

    function show(v) {
        if (v == 1) {
            $("#table").show();
            $("#chartConatiner").hide();
        } else if (v == 2) {
            $("#table").hide();
            $("#chartConatiner").show();
        }
    }
</script>
<?php echo $this->render('/public/footer'); ?>
